import { createStoreBindings } from 'mobx-miniprogram-bindings'
import { store } from '../../store/cart'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: {},
    swiperCurrent: 0,
    shopDetail: [], 
    index: 0,
    opacity: 0,
    show: false,
    selectIndex: 0,
    number: 1,
    toBuy: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function ({skuId, plateForm, spuId}) {
    this._fetchDetail(skuId, plateForm, spuId)
    this.storeBindings = createStoreBindings(this, {
      store,
      fields: ['count', 'login'],
      actions: ['add', 'getUser'],
    })
  },

  onReady: function() {
    this.getUser()
  },

  onShow() {
    this.getUser()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 轮播图索引改变
  handleChangeCurrent(e) {
    this.setData({
      swiperCurrent: e.detail.current
    })
  },

  // 点击返回
  handleToBack(e) {
    wx.navigateBack({
      delta: 0,
    })
  },

  // 点击分享
  handleToShare(e) {
    console.log("点击分享");
  },

  // 点击显示更多品牌保障
  handleToMoreBrand(e) {
    console.log("显示更多保障");
  },

  // 点击切换详情
  handleChangeDetail(e) {
    if(e.currentTarget.id == this.data.index) return
    this.setData({
      index: e.currentTarget.id
    })
  },

  // 页面滚动
  handleScroll(e) {
    let top = e.detail.scrollTop
    if(top > 60 && this.data.opacity === 1) return
    let opacity = Math.min(top / 60, 1)
    this.setData({
      opacity: opacity < 0.2 ? 0 : opacity
    })
  },

  // 点击展示选项
  handleShowSelect(e) {
    if(e.currentTarget.id === '1') this.setData({toBuy: true})
    this.setData({
      show: true,
      toBuy: e.currentTarget.id == 1
    })
  },

  // 选择数量
  handleSelectNumber(e) {
    this.setData({number: e.detail})
  },

  // 加入购物车
  handleAddToCart(e) {
    if(!this.data.login) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return
    }
    let index = Number(e.currentTarget.id)
    let { name, desc, price, specificationImg, skuId, spuId } = this.data.data.skuTradeInfos[index]
    this.add({
      name, 
      desc, 
      price,
      specificationImg, 
      skuId, 
      spuId,
      num: this.data.number, 
      checked: true
    });
    this.handleClose()
  },

  // 去购物车
  handleToCart(e) {
    wx.switchTab({
      url: '/pages/cart/cart',
    })
  },

  // 改变选项
  handleChangeSelect(e) {
    let index = Number(e.currentTarget.id)
    if(index === this.data.selectIndex) return
    this.setData({selectIndex: index})
  },

  // 立即购买
  handleToBuy(e) {
    console.log("立即购买");
  },

  // handleClose
  handleClose(e) {
    this.setData({show: false})
  },
  handleCloseSelect(e) {
    this.handleClose()
  },

  // 请求数据
  _fetchDetail(skuId="", type=1, spuId="") {
    wx.request({
      url: `https://ms.viomi.com.cn/vstore-api/wares/open-api/v1/wares/querySpuAndSkuDetailRefactor?&skuId=${skuId}&spuId=${spuId}&plateForm=${type}`,
      success: (res) => {
        let data = res.data.mobBaseRes.result
        if(data.spuTradeInfo.videoUrl) data.spuTradeInfo.images.unshift('video')
        this.setData({
          data
        })
      }
    })
    wx.request({
      url: `https://ms.viomi.com.cn/wares-web/services/prod/query/queryProdSpuDescImgs.json?skuId=${skuId}&spuId=${spuId}&plateForm=${type}`,
      success: (res) => {
        this.setData({
          shopDetail: res.data.mobBaseRes.result
        })
      }
    })
  }

})